package mine;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Win extends JFrame {

	private static final long serialVersionUID = 1L;
	private JTextArea time;
	private JTextArea number;
	private MineModel mine;
	private Controller controller;

	public Win(Controller controller) {
		this.controller = controller;
		init();
	}

	public void init() {
		setTitle("扫雷");
		setSize(800, 680);
		setResizable(false);
		setContentPane(createBoard());
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				controller.exit();
			}
		});
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension d = toolkit.getScreenSize();
		setLocation((d.width - getWidth()) / 2, (d.height - getHeight()) / 2);

	}

	public void toShow() {
		setVisible(true);
	}

	private Container createBoard() {
		JPanel main = new JPanel(new BorderLayout());
		main.add(BorderLayout.CENTER, createrMine());
		main.add(BorderLayout.EAST, createTool());
		return main;
	}

	private Component createTool() {
		Color color = new Color(140, 160, 220);
		JPanel main = new JPanel(new GridLayout(2, 1, 30, 30));
		main.setBorder(new EmptyBorder(10, 15, 10, 15));
		JPanel main1 = new JPanel(new GridLayout(5, 1, 0, 20));
		main1.setBorder(new LineBorder(new Color(40, 20, 50), 2));
		JLabel timer = new JLabel(" 用时：");
		JLabel num = new JLabel(" 地雷余数：");
		time = new JTextArea(1, 3);
		Font font = new Font(Font.MONOSPACED, Font.TYPE1_FONT, 23);
		time.setText("  0");
		time.setFont(font);
		number = new JTextArea(1, 3);
		number.setFont(font);
		time.setBackground(color);
		number.setBackground(color);
		time.setForeground(new Color(150, 30, 50));
		number.setForeground(new Color(150, 30, 50));
		time.setEditable(false);
		number.setEditable(false);
		main1.add(timer);
		main1.add(time);
		main1.add(num);
		main1.add(number);

		JPanel main2 = new JPanel();
		JPanel main22 = new JPanel(new GridLayout(4, 1, 30, 30));
		main2.setBorder(new LineBorder(new Color(40, 20, 50), 2));
		main22.setBorder(new EmptyBorder(20, 10, 0, 10));
		JButton start = new JButton("开始");

		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.start();
			}
		});

		JButton exit = new JButton("离开");
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.exit();
			}
		});
		JButton button = new JButton("设置");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				OptionWin c = new OptionWin(controller);
				c.toShow();
			}
		});
		
		JButton help = new JButton("帮助");
		help.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.getHelp();
			}
		});
		
		start.setPreferredSize(new Dimension(80, 30));
		exit.setPreferredSize(new Dimension(80, 30));
		button.setPreferredSize(new Dimension(80, 30));
		help.setPreferredSize(new Dimension(80, 30));
		Color c = new Color(100, 225, 207);
		start.setBackground(c);
		exit.setBackground(c);
		button.setBackground(c);
		help.setBackground(c);
		main22.add(start);
		main22.add(button);
		main22.add(help);
		main22.add(exit);
		main.setBackground(color);
		main1.setBackground(color);
		main2.setBackground(color);
		main22.setBackground(color);
		main2.add(main22);
		main.add(main1);
		main.add(main2);
		return main;
	}

	private Component createrMine() {
		JPanel bor = new JPanel(new GridLayout(controller.getBoard().length,
				controller.getBoard()[0].length));
		bor.setBackground(new Color(140, 160, 220));
		int es = (680 - controller.getBoardsize() * 34) / 2;
		bor.setBorder(new LineBorder(new Color(20, 12, 30), 2));
		bor.setBorder(new EmptyBorder(es, es, es, es));

		for (int a = 0; a < controller.getBoard().length; a++) {
			for (int b = 0; b < controller.getBoard().length; b++) {
				controller.getBoard()[a][b] = new MineLabel(a, b, controller);
				controller.getBoard()[a][b].setIcon(MineLabel.zone);
				bor.add(controller.getBoard()[a][b]);
			}
		}
		return bor;
	}

	public MineModel getMine() {
		return mine;
	}

	public void setMine(MineModel mine) {
		this.mine = mine;
	}

	public Controller getController() {
		return controller;
	}

	public void setController(Controller controller) {
		this.controller = controller;
	}

	public JTextArea getTime() {
		return time;
	}

	public void setTime(JTextArea time) {
		this.time = time;
	}

	public JTextArea getNumber() {
		return number;
	}

	public void setNumber(JTextArea number) {
		this.number = number;
	}

}
